#include <iostream>
#include <fstream>
#include <string>

#include "elisa_parser.h"
#include "elisa_lexer.h"

ElisaParser::ElisaParser() 
: m_lexer(new ElisaLexer()),
  m_model(new AndModel())
{}

bool ElisaParser::parseFile(const char *filename) {
	std::ifstream ifs(filename);
	if (!ifs.is_open()) {
		std::cerr << "file " << filename << " does not exist!" << std::endl;
		return 1;
	}
	m_lexer->switch_streams(&ifs, 0);

	elisa::Parser parser(*this);
	return (parser.parse() == 0);
}
	
bool ElisaParser::parseString(const std::string& str) {
	std::istringstream ss(str);
	/*
	m_lexer->switch_streams(&ss, 0);

	elisa::Parser parser(*this);
	return (parser.parse() == 0);
	*/
	return parseStream(ss);
}

bool ElisaParser::parseStream(std::istream& in) {
	m_lexer->switch_streams(&in, 0);

	elisa::Parser parser(*this);
	return (parser.parse() == 0);
}

void ElisaParser::error(const elisa::location& l, const std::string& s) {
	std::cerr << l << ": " << s << std::endl;
}
void ElisaParser::error(const std::string& s) {
	std::cerr << s << std::endl;
}
